/**
 * @author: hankaibo
 */

const helpers = require('./helpers');
const webpackMerge = require('webpack-merge'); // used to merge webpack configs
// const webpackMergeDll = webpackMerge.strategy({
//   plugins: 'replace'
// });
const commonConfig = require('./webpack.common.js'); // the settings that are common to prod and dev

/**
 * Webpack Plugins
 */
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');

/**
 * Webpack Constants
 */
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || 8080;
const PUBLIC = process.env.PUBLIC_DEV || HOST + ':' + PORT;
const AOT = process.env_BUILD_AOT || helpers.hasNpmFlag('aot');
const HMR = helpers.hasProcessFlag('hot');
const METADATA = {
  host: HOST,
  port: PORT,
  public: PUBLIC,
  ENV: ENV,
  HMR: HMR,
  AOT: AOT
};
// const DllBundlesPlugin = require('webpack-dll-bundles-plugin').DllBundlesPlugin;

module.exports = function () {
  return webpackMerge(commonConfig({
    env: ENV
  }), {
    devtool: 'cheap-module-source-map',
    output: {
      path: helpers.root('dist'),
      filename: '[name].bundle.js',
      sourceMapFilename: '[file].map',
      chunkFilename: '[id].chunk.js',
      library: 'ac_[name]',
      libraryTarget: 'var',
    },

    module: {

      rules: [{
          test: /\.css$/,
          use: ['style-loader', 'css-loader'],
          include: [helpers.root('src', 'styles')]
        },
        {
          test: /\.scss$/,
          use: ['style-loader', 'css-loader', 'sass-loader'],
          include: [helpers.root('src', 'styles')]
        },
        {
          test: /\.less$/,
          use: ['style-loader', 'css-loader', 'less-loader'],
          include: [helpers.root('src', 'styles')]
        },

      ]

    },

    plugins: [
      new DefinePlugin({
        'ENV': JSON.stringify(METADATA.ENV),
        'HMR': METADATA.HMR,
        'process.env.ENV': JSON.stringify(METADATA.ENV),
        'process.env.NODE_ENV': JSON.stringify(METADATA.ENV),
        'process.env.HMR': METADATA.HMR
      }),
      // new DllBundlesPlugin({
      //   bundles: {
      //     polyfills: [
      //       'core-js',
      //       {
      //         name: 'zone.js',
      //         path: 'zone.js/dist/zone.js'
      //       },
      //       {
      //         name: 'zone.js',
      //         path: 'zone.js/dist/long-stack-trace-zone.js'
      //       },
      //     ],
      //     vendor: [
      //       '@angular/platform-browser',
      //       '@angular/platform-browser-dynamic',
      //       '@angular/core',
      //       '@angular/common',
      //       '@angular/forms',
      //       '@angular/http',
      //       '@angular/router',
      //       '@angularclass/hmr',
      //       'rxjs',
      //       'material-design-lite'
      //     ]
      //   },
      //   dllDir: helpers.root('dll'),
      //   webpackConfig: webpackMergeDll(commonConfig({
      //     env: ENV
      //   }), {
      //     devtool: 'cheap-module-source-map',
      //     plugins: []
      //   })
      // }),
      // new AddAssetHtmlPlugin([{
      //     filepath: helpers.root(`dll/${DllBundlesPlugin.resolveFile('polyfills')}`)
      //   },
      //   {
      //     filepath: helpers.root(`dll/${DllBundlesPlugin.resolveFile('vendor')}`)
      //   }
      // ]),
      // new NamedModulesPlugin(),
      new LoaderOptionsPlugin({
        debug: true,
        options: {

        }
      }),
      new HotModuleReplacementPlugin()

    ],
    devServer: {
      port: METADATA.port,
      host: METADATA.host,
      hot: METADATA.HMR,
      public: METADATA.public,
      historyApiFallback: true,
      watchOptions: {
        ignored: /node_modules/
      },
      setup: function (app) {

      },
      proxy: {
        "/api": {
          target: "http://localhost:3000",
          pathRewrite: {
            "^/api": ""
          }
        }
      }
    },
    node: {
      global: true,
      crypto: 'empty',
      process: true,
      module: false,
      clearImmediate: false,
      setImmediate: false
    }
  });
};
